package com.banshuibao.bsb_android.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.download.DownloadedAudioFragment;
import com.banshuibao.bsb_android.download.DownloadingAudioFragment;

/**
 * Created by zyh on 2018/6/13.
 */

public class RadioTabDownloadFragment extends BaseFragment {

    public RadioTabDownloadFragment() {
    }

    private FragmentManager manager;
    private LinearLayout l1, l2;
    private TextView title1, title2;
    private View line_1, line_2;
    private int type = R.id.title_1;
    private DownloadingAudioFragment downloadingFragment;
    private DownloadedAudioFragment downloadedFragment;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_media_tab, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        title1 = view.findViewById(R.id.title_text1);
        title2 = view.findViewById(R.id.title_text2);
        l1 = view.findViewById(R.id.title_1);
        l2 = view.findViewById(R.id.title_2);
        line_1 = view.findViewById(R.id.line_1);
        line_2 = view.findViewById(R.id.line_2);
        l1.setOnClickListener(l);
        l2.setOnClickListener(l);
        showFragment(R.id.title_1);
    }

    private View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == type)
                return;
            type = v.getId();
            if (type == R.id.title_1) {
                title1.setTextColor(getResources().getColor(R.color.line_blue));
                title2.setTextColor(getResources().getColor(R.color.text_black));
                line_1.setBackgroundResource(R.color.line_blue);
                line_2.setBackgroundResource(R.color.line_color);
            } else {
                title1.setTextColor(getResources().getColor(R.color.text_black));
                title2.setTextColor(getResources().getColor(R.color.line_blue));
                line_1.setBackgroundResource(R.color.line_color);
                line_2.setBackgroundResource(R.color.line_blue);
            }
            showFragment(type);
        }
    };

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (downloadedFragment != null)
            downloadedFragment.onHiddenChanged(hidden);
        if (downloadingFragment != null)
            downloadingFragment.onHiddenChanged(hidden);
    }

    private void showFragment(int id) {
        manager = getChildFragmentManager();
        hideFragment(downloadedFragment, downloadingFragment);
        FragmentTransaction transaction = manager.beginTransaction();
        switch (id) {
            case R.id.title_1:
                if (downloadedFragment == null) {
                    downloadedFragment = new DownloadedAudioFragment();
                    transaction.add(R.id.fragment, downloadedFragment);
                } else
                    transaction.show(downloadedFragment);
                break;
            case R.id.title_2:
                if (downloadingFragment == null) {
                    downloadingFragment = new DownloadingAudioFragment();
                    transaction.add(R.id.fragment, downloadingFragment);
                } else
                    transaction.show(downloadingFragment);
                break;
        }
        transaction.commit();
    }

    private void hideFragment(Fragment... fragments) {
        for (Fragment fragment : fragments) {
            if (fragment != null)
                manager.beginTransaction().hide(fragment).commit();
        }
    }

}
